package com.caishi.lkx.user.service.impl;

import com.caishi.lkx.user.ienum.status.UserStatus;
import com.caishi.lkx.user.ienum.type.UserAccountType;
import com.caishi.lkx.user.ienum.type.UserType;
import com.caishi.lkx.user.mapper.GroupMapper;
import com.caishi.lkx.user.model.*;
import com.caishi.lkx.user.service.*;

import com.zzw.common.entity.IBaseMapper;


import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.annotation.Resource;


@Service
@Slf4j
public class GroupServiceImpl implements IGroupService {

    @Resource
    private GroupMapper groupMapper;

    @Resource
    private IChannelBusinessService channelBusinessService;

    @Override
    public IBaseMapper<GroupModel, Long> getMapper() {
        return groupMapper;
    }


    @SneakyThrows
    @Override
    @Transactional
    public GroupModel update(GroupModel entity) {
        if(entity.getId() == null || entity.getId() == 0L){
            getMapper().insert(entity);

            ChannelBusinessModel  cb =  entity.getChannelBusiness();
            cb.setUserType(UserType.channelBusiness.getValue());
            cb.setUserStatus(UserStatus.normal);
            cb.setGroupId(entity.getId());
            cb.setName(entity.getName());
            var user =  channelBusinessService.addUser(cb, UserAccountType.username);
            entity.setManagerId(user.getId());
        }



        getMapper().updateById(entity);
        return entity;
    }
}
